﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using Vit.Common.Extensions;

namespace Vit.Common.Helpers
{
    public class ImageHelper
    {
        public static Image ResizeImage(Image originalImage, int width, int height, ResizeMode mode, int scale)
        {
            if (width == 0 || height == 0)
                return originalImage;
            Size newSize = originalImage.Size.Resize(width, height, mode, scale);
            int x = 0;
            int y = 0;
            if (mode == ResizeMode.Crop)
            {
                x = originalImage.Width - newSize.Width / 2;
                y = originalImage.Height - newSize.Height / 2;
            }

            //新建一个bmp图片
            Image bitmap = new System.Drawing.Bitmap(newSize.Width, newSize.Height);

            //新建一个画板
            Graphics g = System.Drawing.Graphics.FromImage(bitmap);

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(originalImage, new Rectangle(0, 0, newSize.Width, newSize.Height),
                new Rectangle(x, y, newSize.Width, newSize.Height),
                GraphicsUnit.Pixel);

            g.Dispose();
            return bitmap;
        }

        public static void WaterMark(Image image, Image watermarkImage, Point offset, ContentAlignment imagePosition)
        {
            using (Graphics newGp = Graphics.FromImage(image))
            {
                newGp.CompositingQuality = CompositingQuality.HighQuality;

                //设置高质量插值法
                newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                //设置高质量,低速度呈现平滑程度
                newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
                Rectangle newRect = rect.Resize(offset, new Size(watermarkImage.Width, watermarkImage.Height), imagePosition); 

                newGp.DrawImage(watermarkImage, newRect);
            }
        }

        public static void WriteStringImage(Image image, string waterStr, Font font, Brush brush, Point p)
        {
            using (Graphics newGp = Graphics.FromImage(image))
            {
                Int32 stringWidth;
                Int32 stringHeight;
                stringHeight = (int)font.Size;
                stringWidth = (int)((font.Size + 1));

                newGp.CompositingQuality = CompositingQuality.HighQuality;

                //设置高质量插值法
                newGp.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                //设置高质量,低速度呈现平滑程度
                newGp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

                //文字抗锯齿
                newGp.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                newGp.DrawString(waterStr, font, brush, p);
            }
        }
    }
}
